/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using QName = System.Qizx.Api.QName;
namespace System.Qizx.Xdm {

    /// <summary>
    /// EXternal representation of a QName.
    /// Not guaranteed unique, comparison cannot be performed by ==.
    /// </summary>
    public class XQName : QName {
        public override string Prefix {
            get {
                return _prefix;
            }

        }
        private static System.Collections.Hashtable prefixedNameCache = new System.Collections.Hashtable();

        private string _prefix;

        public XQName(string namespaceURI, string localName, string prefix)
            : base(localName, namespaceURI) {
            this._prefix = prefix;
        }

        /// <summary> Obtains a cached representation of a prefixed QName from a
        /// QName and a prefix.
        /// </summary>
        public static XQName Get(QName name, string prefix) {
            lock (typeof(System.Qizx.Xdm.XQName)) {
                XQName n = (XQName)prefixedNameCache[name];
                if (n != null && prefix != null) {
                    string prefix2 = n.Prefix;
                    if (prefix2 != null && prefix.Equals(prefix2))
                        return n;
                }
                XQName res = new XQName(name.Namespace, name.Name, prefix);
                prefixedNameCache[res] = res;
                return res;
            }
        }

        public static XQName Get(string namespaceURI, string localPart, string prefix) {
            lock (typeof(System.Qizx.Xdm.XQName)) {
                return Get(IQName.GetQName(namespaceURI, localPart), prefix);
            }
        }

        public override string ToString() {
            if (this.Namespace.Length == 0)
                return this.Name;
            return '{' + Namespace + '}' + Name;
        }
    }
}